package cn.changshengjue.dakong.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageUtil {

	/**
	 * 开始分页
	 *
	 * @param list
	 * @param pageNum  页码
	 * @param pageSize 每页多少条数据
	 * @return
	 */
	public static List<Map<String, Object>> startPage(List<Map<String, Object>> list, Integer pageNum,
			Integer pageSize) {
		if (list == null) {
			return null;
		}
		if (list.size() == 0) {
			return null;
		}

		Integer count = list.size(); // 记录总数
		Integer pageCount = 0; // 页数
		if (count % pageSize == 0) {
			pageCount = count / pageSize;
		} else {
			pageCount = count / pageSize + 1;
		}

		int fromIndex = 0; // 开始索引
		int toIndex = 0; // 结束索引

		if (pageNum > pageCount) {
			pageNum = pageCount;
		}
		if (!pageNum.equals(pageCount)) {
			fromIndex = (pageNum - 1) * pageSize;
			toIndex = fromIndex + pageSize;
		} else {
			fromIndex = (pageNum - 1) * pageSize;
			toIndex = count;
		}

		List<Map<String, Object>> pageList = list.subList(fromIndex, toIndex);

		return pageList;
	}

	/**
	 * 使用分页util的
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < ((54 * 3) - 10); i++) {
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("1", "a");
			list.add(hashMap);
		}
		Integer pageNum = 3;
		Integer pageSize = 54;
		List startPage = startPage(list, pageNum, pageSize);
		System.out.println(Math.ceil(Double.valueOf(list.size()) / Double.valueOf(54)));
		System.out.println(startPage);
	}

}
